package com.es.disped.web.controller.oj.problem;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.oj.problem.HustProblemService;
import com.es.disped.common.annotation.DynamicDataSource;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.model.PageModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.oj.model.Problem;
import com.es.disped.dao.oj.model.ProblemExample;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Anson<br>
 *	Copyright by EasyShare 2019<br>
 *  
 *  All right reserved<br>
 *
 *  Created on 2019年7月16日 上午12:57:21<br>
 *  名称：ProblemController.java<br>
 *  描述：OJ题库管理<br>
 */
@Api("OJ题库管理")
@Controller
@RequestMapping("/oj/problem")
public class ProblemController extends BaseController{
	
	@Autowired
	HustProblemService hustProblemService;

	@ApiOperation(value="检索OJ题库",response=MsgModel.class,httpMethod="POST",notes="检索OJ题库")
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel list(@RequestBody @ApiParam(name="problem",value="OJ题库对象",required=false) Problem problem)
	{
		ProblemExample listExample=new ProblemExample();
		ProblemExample.Criteria criteria=listExample.createCriteria();
		criteria.andDefunctEqualTo("N");
		if(!this.isNull(problem.getProblemId()))
		{
			criteria.andProblemIdEqualTo(problem.getProblemId());
		}
		if(!this.isNull(problem.getTitle()))
		{
			criteria.andTitleLike("%"+problem.getTitle()+"%");
		}
		listExample.setOrderByClause("problem_id asc");
		List<Problem> list=hustProblemService.selectByExample(listExample);
		return this.resultMsg(!this.isNull(list)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(list)?"OJ题库列表加载成功":"OJ题库列表加载失败",!this.isNull(list)?list:"");
	}
	
	
	@ApiOperation(value="分页加载OJ题库",response=MsgModel.class,httpMethod="POST",notes="分页加载OJ题库，分页参数['offset','limit']")
	@RequestMapping(value="/pages",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel pages(@RequestBody @ApiParam(name="paramMap",value="分页参数[offset,limit]",required=true) Map<String, Object> paramMap)
	{
		ProblemExample listExample=new ProblemExample();
		PageModel<Problem> pageModel=null;
		if(Optional.of(paramMap)
				.filter(offset->paramMap.containsKey("offset")!=false)
				.filter(limit->paramMap.containsKey("limit")!=false)
				.isPresent())
		{
			listExample.setOrderByClause("problem_id asc");
			pageModel=hustProblemService.selectByExampleForOffsetPage(listExample,
					Integer.valueOf(paramMap.get("offset").toString()), Integer.valueOf(paramMap.get("limit").toString()));
			
		}
		return this.resultMsg(!this.isNull(pageModel)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(pageModel)?"OJ题库加载成功":"OJ题库加载失败",!this.isNull(pageModel)?pageModel:"");
	}
	
	
	
	@ApiOperation(value="获取题目描述",response=MsgModel.class,httpMethod="POST",notes="通过题目ID获取题目描述内容")
	@RequestMapping(value="/getProblemDetail",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel getProblemDetail(@RequestBody @ApiParam(name="problem",value="OJ题库对象",required=false) Problem problem)
	{
		ProblemExample problemExample=new ProblemExample();
		ProblemExample.Criteria criteria=problemExample.createCriteria();
		criteria.andDefunctEqualTo("N").andProblemIdEqualTo(problem.getProblemId());
		Problem problemDetail=hustProblemService.selectFirstByExampleWithBLOBs(problemExample);
		return this.resultMsg(!this.isNull(problemDetail)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(problemDetail)?"OJ题库列表加载成功":"OJ题库列表加载失败",!this.isNull(problemDetail)?problemDetail:"");
	}
	
	
	
}
